! function() {
    window.setTimeout(
        function() {
            AnimationWhenScroll()
        },
        1000
    )

    window.addEventListener('scroll', function(e) {
        //滚动动画
        AnimationWhenScroll()
    })

    //导航自动高亮,段落提升
    function AnimationWhenScroll() {
        let specialTags = document.querySelectorAll('[data-x]') //获取session

        let minIndex = findClosestSession(specialTags) //定位最近session
        specialTags[minIndex].classList.remove('animate')

        highLightNav(specialTags, minIndex) //导航自动高亮
    }

    //找当前浏览位置最近的session元素
    function findClosestSession(sessions) {
        let minIndex = 0
        for (let i = 0; i < sessions.length; i++) {
            if (Math.abs(sessions[i].offsetTop - window.scrollY) < Math.abs(sessions[minIndex].offsetTop - window.scrollY)) {
                minIndex = i
            }
        }
        return minIndex
    }

    //根据当前浏览位置高亮导航
    function highLightNav(sessions, minIndex) {
        let id = sessions[minIndex].id
        let a = document.querySelector('a[href="#' + id + '"]')
        let li = a.parentNode
        let lis = li.parentNode.children
        for (let i = 0; i < lis.length; i++) {
            lis[i].classList.remove('highLight')
        }
        li.classList.add('highLight')
    }
}.call()